package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.*;
import com.example.springsecurity.mapper.StudentExperimentMapper;
import com.example.springsecurity.service.StudentExperimentService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.example.springsecurity.utils.redis.RedisCache;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class StudentExperimentServiceImpl implements StudentExperimentService {

    @Autowired
    public StudentExperimentMapper studentExperimentMapper;


    @Autowired
    public RedisCache redisCache;

    @Override
    public ResultBean searchAllExperiment(Integer collegeId, Integer majorId, String projectName,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoProject> projectList=studentExperimentMapper.searchAllExperiment(collegeId,majorId,projectName);
        PageInfo<RoProject> roProjectPageInfo=new PageInfo<>(projectList);
        return ResultBean.success(roProjectPageInfo);
    }


    @Override
    public ResultBean getExperimentDetail(Integer projectId) {
        Integer doExperimentNums=redisCache.getCacheObject("doExperiment:"+projectId);
        if(doExperimentNums==null){
            doExperimentNums=0;
            redisCache.setCacheObject("doExperiment:"+projectId,doExperimentNums);
        }
       Integer searchExperimentNums= redisCache.getCacheObject("searchExperiment:"+projectId);
       if(searchExperimentNums==null){
           searchExperimentNums=1;
       }else{
           searchExperimentNums++;
       }
       redisCache.setCacheObject("searchExperiment:"+projectId,searchExperimentNums);
       RoProject roProject=studentExperimentMapper.getExperimentDetail(projectId);
       List<LearnMaterials> learnMaterialsList=studentExperimentMapper.getLeanMaterials(projectId);
        ExperimentDetailResult experimentDetailResult=new ExperimentDetailResult();
        experimentDetailResult.setRoProject(roProject);
        experimentDetailResult.setLearnMaterialsList(learnMaterialsList);
        experimentDetailResult.setDoExperimentNums(doExperimentNums);
        experimentDetailResult.setSearchExperimentNums(searchExperimentNums);
        return ResultBean.success(experimentDetailResult);
    }


    @Override
    public ResultBean getExperimentVideo(Integer projectId) {
        Video video=studentExperimentMapper.getExperimentVideo(projectId);//根据项目编号来获取到视频学习的一系列资料
        return ResultBean.success(video);
    }


    @Override
    public ResultBean doExperiment(Integer projectId, Integer status,SysUser sysUser,Integer assignId) {
        RoProject roProject=studentExperimentMapper.getExperimentReportContent(projectId);
        Integer doExperimentNums=redisCache.getCacheObject("doExperiment:"+projectId);
        doExperimentNums++;
        redisCache.setCacheObject("doExperiment:"+projectId,doExperimentNums);
        Integer submitId=0;
        //0为选修，1为必修
        if(status==0){
            ScoreSubmit scoreSubmit=new ScoreSubmit();
            scoreSubmit.setStudentId(sysUser.getUserId().intValue());
            scoreSubmit.setUsername(sysUser.getUserName());
            scoreSubmit.setExperimentId(projectId);
            if(StringUtils.isNotNull(roProject.getExperimentReportTemplate())){
                scoreSubmit.setExperimentContent(roProject.getExperimentReportTemplate());
            }
            studentExperimentMapper.addScoreSubmit(scoreSubmit);
            submitId=scoreSubmit.getSubmitId();
        }else{
            StudentMustScoreSubmit studentMustScoreSubmit=new StudentMustScoreSubmit();
            studentMustScoreSubmit.setStudentId(sysUser.getUserId().intValue());
            studentMustScoreSubmit.setUsername(sysUser.getUserName());
            studentMustScoreSubmit.setExperimentId(projectId);
            studentMustScoreSubmit.setAssignId(assignId);
            if(StringUtils.isNotNull(roProject.getExperimentReportTemplate())){
                studentMustScoreSubmit.setMustExperimentContent(roProject.getExperimentReportTemplate());
            }
            studentExperimentMapper.addStudentScoreSubmit(studentMustScoreSubmit);

            studentExperimentMapper.updateAssignExperiment(sysUser.getUserName(),assignId);
            submitId=studentMustScoreSubmit.getSubmitId();
        }
        return ResultBean.success(submitId);
    }


    @Override
    public ResultBean submitVideoScore(Integer submitId, Boolean videoStatus,Integer status) {
        Integer submitVideoScoreStatus=0;
        if(status==0){
            submitVideoScoreStatus=studentExperimentMapper.submitVideoScore(submitId,videoStatus);
        }else{
            submitVideoScoreStatus=studentExperimentMapper.submitMustVideoScore(submitId,videoStatus);
        }
        if(submitVideoScoreStatus!=0){
            return ResultBean.success("提交成功");
        }else{
            return ResultBean.error("提交失败");
        }
    }

    @Override
    public ResultBean getAddressLink(Integer submitId,Integer status) {
        RoProject roProject=new RoProject();
        if(status==0){
            roProject=studentExperimentMapper.getAddressLink(submitId);
        }else{
            roProject=studentExperimentMapper.getMustAddressLink(submitId);
        }
        return ResultBean.success(roProject);
    }


    @Override
    public ResultBean getExperimentReportContent(Integer projectId) {
        RoProject roProject=studentExperimentMapper.getExperimentReportContent(projectId);
        return ResultBean.success(roProject);
    }


    @Override
    public ResultBean updateVirtualExperiment(Integer submitId, Integer virtualExperimentScore,Integer status,String value) {
        Integer updateVirtualExperimentStatus=0;
        try {
            if(status==0){
                updateVirtualExperimentStatus=studentExperimentMapper.updateVirtualExperiment(submitId,virtualExperimentScore,value);
            }else if(status==1){
                updateVirtualExperimentStatus=studentExperimentMapper.updateMustVirtualExperiment(submitId,virtualExperimentScore,value);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("提交失败"+submitId+"提交编号不存在");
        }
        if(updateVirtualExperimentStatus!=0){
            return ResultBean.success("成功提交虚拟仿真实验成绩");
        }else{
            return ResultBean.error("提交虚拟仿真实验成绩失败");
        }
    }


    @Override
    public ResultBean submitExperiment(Integer submitId, String experimentContent, Integer submitStatus) {
        ScoreSubmit scoreSubmit=new ScoreSubmit();
        scoreSubmit.setSubmitId(submitId);
        scoreSubmit.setExperimentContent(experimentContent);
        scoreSubmit.setSubmitStatus(submitStatus);
        if(submitStatus==1){
            scoreSubmit.setSubmitTime(new Date());
        }
        Integer submitExperimentStatus=studentExperimentMapper.submitExperiment(scoreSubmit);
        if(submitExperimentStatus!=0){
            return ResultBean.success("保存成功");
        }else{
            return ResultBean.error("保存失败");
        }
    }


    @Override
    public ResultBean getStudentOptionalExperiment(Integer pageNum, Integer pageSize,String projectName,Integer userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoProject> roProjectList=studentExperimentMapper.getStudentOptionalExperiment(projectName,userId);
        PageInfo<RoProject> roProjectPageInfo=new PageInfo<>(roProjectList);
        return ResultBean.success(roProjectPageInfo);
    }


    @Override
    public ResultBean getStudentExperimentRecord(Integer pageNum, Integer pageSize, Long userId,Integer projectId) {
        PageHelper.startPage(pageNum,pageSize);
        List<ScoreSubmit> scoreSubmitList=studentExperimentMapper.getStudentExperimentRecord(userId,projectId);
        PageInfo<ScoreSubmit> scoreSubmitPageInfo=new PageInfo<>(scoreSubmitList);
        return ResultBean.success(scoreSubmitPageInfo);
    }

    @Override
    public ResultBean getStudentMustExperiment(SysUser sysUser, String projectName, Integer accomplishCondition,Integer pageNum,Integer pageSize) {
        List<AssignExperiment> assignExperimentList;
        if(accomplishCondition==0){
            PageHelper.startPage(pageNum,pageSize);
            /**
             * 如果是0状态
             */
            PageHelper.startPage(pageNum,pageSize);
             assignExperimentList=studentExperimentMapper.getStudentMustExperimentByZero(sysUser.getUserName(),projectName,accomplishCondition);
        }else{
            PageHelper.startPage(pageNum,pageSize);
            assignExperimentList=studentExperimentMapper.getStudentMustExperiment(sysUser.getUserName(),projectName,accomplishCondition);

        }
        PageInfo<AssignExperiment> assignExperimentPageInfo=new PageInfo<>(assignExperimentList);
        return ResultBean.success(assignExperimentPageInfo);
    }


    @Override
    public ResultBean submitMustExperiment(StudentMustScoreSubmit studentMustScoreSubmit) {
        Integer submitMustExperimentStatus=studentExperimentMapper.submitMustExperiment(studentMustScoreSubmit);//学生用户先去提交习题完成情况
        StudentMustScoreSubmit assignStudentMustExperiment=studentExperimentMapper.assignStudentMustExperiment(studentMustScoreSubmit.getSubmitId());//根据提交编号来获取到提交信息的的详细情况
        if(submitMustExperimentStatus!=0){
            AssignExperimentUser assignExperimentUser=new AssignExperimentUser();
            assignExperimentUser.setAssignId(assignStudentMustExperiment.getAssignId());
            assignExperimentUser.setUsername(assignStudentMustExperiment.getUsername());
            /**
             * 根据学生用户完成的必修实验的情况如果提交状态为0的话就是待完成，为1的话就是待批改
             */
            if(studentMustScoreSubmit.getSubmitStatus()==0){
                assignExperimentUser.setAccomplishCondition(1);
            }else if(studentMustScoreSubmit.getSubmitStatus()==1){
                assignExperimentUser.setAccomplishCondition(2);
            }
            studentExperimentMapper.updateAssignExperimentCondition(assignExperimentUser);//根据布置的编号跟用户名来修改布置信息的编号
            return ResultBean.success("提交成功");
        }else{
            return ResultBean.error("提交失败");
        }
    }

    @Transactional
    @Override
    public ResultBean revocationCorrect(Integer assignId,String username) {
        Integer revocationCorrectStatus=studentExperimentMapper.revocationCorrect(assignId,username);
        Integer updateSubmitStatus=studentExperimentMapper.updateSubmitStatus(assignId);
        if(revocationCorrectStatus!=0&&updateSubmitStatus!=0){
            return ResultBean.success("撤销批改成功!");
        }else{
            return ResultBean.error("撤销批改失败!");
        }
    }




    @Override
    public ResultBean getExperimentShareRecord(Integer submitId,Integer status) {
        ResultExperimentScore experimentScore=new ResultExperimentScore();
        if(status==0){
             experimentScore=studentExperimentMapper.getExperimentShareRecord(submitId);//选修已经批改的成绩分数
        }else{
            experimentScore=studentExperimentMapper.getExperimentCorrectRecord(submitId);//必修已经批改的成绩分数
        }
        return ResultBean.success(experimentScore);

    }


    @Override
    public ResultBean getSubmitContent(Integer submitId) {
        String experimentContent="";
        // if(status==0){
            experimentContent=studentExperimentMapper.getChooseContent(submitId);//获取到选修的实验报告内容
        // }else{
        //     experimentContent=studentExperimentMapper.getMustContent(submitId);//获取到必修的实验报告内容
        // }
        return ResultBean.success("",experimentContent);
    }


    @Override
    public ResultBean getExperimentSubmit(Integer status, Integer submitId) {
        ResultExperimentSubmit resultExperimentSubmit=new ResultExperimentSubmit();
        ////0为选修，1为必修
        if(status==0){
            ScoreSubmit scoreSubmit=studentExperimentMapper.getScoreSubmit(submitId);
            resultExperimentSubmit.setVideoStatus(scoreSubmit.getVideoStatus());
            resultExperimentSubmit.setExperimentTemplate(scoreSubmit.getExperimentContent());
        }else{
            StudentMustScoreSubmit studentMustScoreSubmit=studentExperimentMapper.getMustScoreSubmit(submitId);
            resultExperimentSubmit.setVideoStatus(studentMustScoreSubmit.getMustVideoStatus());
            resultExperimentSubmit.setExperimentTemplate(studentMustScoreSubmit.getMustExperimentContent());
        }
        return ResultBean.success(resultExperimentSubmit);
    }


    @Override
    public ResultBean updateSubmitDate(Integer status, Integer submitId) {
        Integer updateSubmitDate=0;
        if(status==0){
            updateSubmitDate=studentExperimentMapper.updateExperimentDate(submitId);
        }else{
            updateSubmitDate=studentExperimentMapper.updateMustExperimentDate(submitId);
        }
        if(updateSubmitDate!=0){
            return ResultBean.success("成功更新实验开始时间");
        }else{
            return ResultBean.error("更新实验开始时间失败");
        }
    }


    public static void main(String[] args) {
        Map<Integer,String> map=new HashMap<>();
        map.keySet().stream().forEachOrdered(item-> System.out.println(map.get("")));
    }
}
